package com.flink.java;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

public class batchWordCount {
    public static void main(String[] args) throws Exception {
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        DataSource<String> dataSource = env.readTextFile("doit40/   data/Hero.txt");
        dataSource.flatMap(new myflatmapfunction())
                .groupBy(0)
                .sum(1)
                .print();
    }

}
class myflatmapfunction implements FlatMapFunction<String, Tuple2<String,Integer>>{
    @Override
    public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
        String[] words = value.split(",");
        for (String word : words) {
            out.collect(Tuple2.of(word,1));
        }
    }
}
